package c.c.a;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.Looper;
import java.util.concurrent.CountDownLatch;

public final class k1<SERVICE, RESULT> {
    public final CountDownLatch a = new CountDownLatch(1);
    public final Intent b;
    public final b<SERVICE, RESULT> c;
    public final Context d;

    public class a implements ServiceConnection {
        public final CountDownLatch a;
        public final b<SERVICE, RESULT> b;
        public SERVICE c;

        public a(k1 k1Var, CountDownLatch countDownLatch, b<SERVICE, RESULT> bVar) {
            this.a = countDownLatch;
            this.b = bVar;
        }

        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            String str = c1.j;
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("ServiceBlockBinder#onServiceConnected ");
            stringBuilder.append(componentName);
            u0.a(str, stringBuilder.toString(), null);
            try {
                this.c = this.b.a(iBinder);
                try {
                    this.a.countDown();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } catch (Throwable th) {
                try {
                    this.a.countDown();
                } catch (Exception e2) {
                    e2.printStackTrace();
                }
                throw th;
            }
        }

        public void onServiceDisconnected(ComponentName componentName) {
            String str = c1.j;
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("ServiceBlockBinder#onServiceDisconnected");
            stringBuilder.append(componentName);
            u0.a(str, stringBuilder.toString(), null);
            try {
                this.a.countDown();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public interface b<T, RESULT> {
        T a(IBinder iBinder);

        RESULT a(T t);
    }

    public k1(Context context, Intent intent, b<SERVICE, RESULT> bVar) {
        this.d = context;
        this.b = intent;
        this.c = bVar;
    }

    public RESULT a() {
        Throwable th;
        if (Looper.getMainLooper() == Looper.myLooper()) {
            return null;
        }
        a aVar;
        try {
            aVar = new a(this, this.a, this.c);
            this.d.bindService(this.b, aVar, 1);
            this.a.await();
            try {
                Object a = this.c.a(aVar.c);
                b(aVar);
                return a;
            } catch (Throwable th2) {
                th = th2;
                try {
                    th.printStackTrace();
                    return null;
                } finally {
                    b(aVar);
                }
            }
        } catch (Throwable th3) {
            th = th3;
            aVar = null;
            th.printStackTrace();
            return null;
        }
    }

    public final void b(a aVar) {
        if (aVar != null) {
            try {
                this.d.unbindService(aVar);
            } catch (Throwable th) {
                th.printStackTrace();
            }
        }
    }
}
